package com.withoutwords.ui;

import com.samsung.sdraw.SettingView;
import com.samsung.spensdk.SCanvasView;
import com.samsung.spensdk.applistener.SCanvasInitializeListener;
import com.withoutwords.R;
import com.withoutwords.engine.GameController;
import com.withoutwords.engine.Round;
import com.withoutwords.engine.RoundMediaTime;
import com.withoutwords.engine.RoundMediaType;
import com.withoutwords.ui.custom.IWordsLayoutListener;
import com.withoutwords.ui.custom.WordsLayout;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public abstract class BasePlayinyActivity extends BaseActivity implements OnClickListener, SCanvasInitializeListener, IWordsLayoutListener
{
	
	protected SCanvasView mCanvasView;
	protected SettingView mSettingView;
	//Mode mode;
	protected Round round = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
		round = (Round)getIntent().getSerializableExtra("round");
		
	}
	int getMediaTimeResource(RoundMediaTime time)
	{
		int resID = 0;
		switch(time)
		{}
		return resID;
	}
	int getMediaInfoResource(RoundMediaType type,RoundMediaTime time)
	{
		int resID = 0;
	if(type == RoundMediaType.Series && time == RoundMediaTime.Old)
		resID = R.drawable.series_old;
	else if(type == RoundMediaType.Series && time == RoundMediaTime.New)
		resID = R.drawable.series_new;
	else if(type == RoundMediaType.Series && time == RoundMediaTime.Recent)
		resID = R.drawable.series_current;
	else if(type == RoundMediaType.Play && time == RoundMediaTime.Old)
		resID = R.drawable.play_old;
	else if(type == RoundMediaType.Play && time == RoundMediaTime.Recent)
		resID = R.drawable.play_current;
	else if(type == RoundMediaType.Play && time == RoundMediaTime.New)
		resID = R.drawable.play_new;
	else if(type == RoundMediaType.Song && time == RoundMediaTime.Old)
		resID = R.drawable.song_old;
	else if(type == RoundMediaType.Song && time == RoundMediaTime.Recent)
		resID = R.drawable.song_current;
	else if(type == RoundMediaType.Song && time == RoundMediaTime.New)
		resID = R.drawable.song_new;
	else if(type == RoundMediaType.Film && time == RoundMediaTime.Old)
		resID = R.drawable.film_old;
	else if(type == RoundMediaType.Film && time == RoundMediaTime.Recent)
		resID = R.drawable.film_current;
	else if(type == RoundMediaType.Film && time == RoundMediaTime.New)
		resID = R.drawable.film_new;
	
	
	
		return resID;
	}
	protected void initViews()
	{
		ImageView imgView = (ImageView)findViewById(R.id.imgViewMediaInfo);
		imgView.setImageResource(getMediaInfoResource(round.getMediaType(),round.getMediaTime()));
	//	imgView.setBackgroundResource(getMediaTypeResource(round.getMediaType()));
		
		
		initDrawingViews();
		initMediaNameView(true);
	}
	protected void initDrawingViews()
	{
		FrameLayout fly =  (FrameLayout)findViewById(R.id.canvas_container_id);

		//fly.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		mCanvasView = new SCanvasView(this);
		mSettingView = new SettingView(this);
		
		
		mCanvasView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		mSettingView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		mCanvasView.setSCanvasInitializeListener(this);
		mCanvasView.setBGColor(Color.WHITE);
		fly.addView(mCanvasView);
		fly.addView(mSettingView);
		//return fly;
	}
	static int id = 0x7f06001f;

	// Returns a valid id that isn't in use
	public int findId(){  
	    View v = findViewById(id);  
	    while (v != null){  
	        v = findViewById(++id);  
	    }  
	    return id++;  
	}
	protected void initMediaNameView(boolean bEnabled)
	{
		WordsLayout wly = (WordsLayout)findViewById(R.id.text_container_id);
		wly.createViews(round.getWords(),300);
		wly.setListener(this);
		wly.setEnabled(bEnabled);
	}
	/*protected void initMediaNameViews(String mediaName)
	{
		String[] words = mediaName.split(" ");
		
		TextView tv = new TextView(this);	
		tv.setTextAppearance(this, android.R.style.TextAppearance_Large);
		float [] wordsWidth = new float[words.length];
		
		
		RelativeLayout rly = (RelativeLayout)findViewById(R.id.text_container_id);
	//	rly.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				//
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		int tvId = 0;
		
		int width = metrics.widthPixels;
		float nOffset = 0;
		
		for(int i =0 ; i< words.length ; i++)
		{
			wordsWidth[i] = tv.getPaint().measureText(words[i]);
			float nEnd  = nOffset + wordsWidth[i]+30;// 20 for padding;
			boolean newLine = false;
			if(nEnd > width)
			{
				nOffset = 0;
				newLine = true;
			}
			else
			{
				nOffset += wordsWidth[i]+30;
			}
			TextView tvWord = new TextView(this);	
			tvWord.setTextAppearance(this, android.R.style.TextAppearance_Large);
			tvWord.setId(findId());
			tvWord.setPadding(10,10, 10, 10);
			RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			//rlp.addRule(verb, anchor)
			if( i==0 )
			{
				//rlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
			}
			else 
			{
				if(newLine)
				{
					rlp.addRule(RelativeLayout.BELOW, tvId);
					rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
				}
				else
				{
					rlp.addRule(RelativeLayout.RIGHT_OF, tvId);
					rlp.addRule(RelativeLayout.ALIGN_TOP,tvId);
				}
			}
			
			tvWord.setLayoutParams(rlp);
			tvWord.setText(words[i]);
			tvWord.setOnClickListener(this);
			rly.addView(tvWord);
			
			//save the id for the next run
			tvId = tvWord.getId();

		}
	//	return rly;
	}*/

}
